<?php
/**
 * author by OCY, 2022/01/19 16:09.
 */

namespace app\index\controller;

use think\Db;
use think\Exception;
use think\Request;

class Plan
{
    // 过期服务处理
    public function doExpireService()
    {
        file_put_contents(RUNTIME_PATH . 'expire_service.log', date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
        $list = Db::name('user_service')
            ->where('expire_time', '>', 0)
            ->where('expire_time', '<=', time())
            ->where('edit_expire', 0)
            ->select();

        foreach ($list as $val) {
            // 修改成过期
            Db::name('user_service')->where(['id' => $val['id']])->setField('edit_expire', 1);

            // 过期之后该用户之前看过的用户资料全部失效
            Db::name('user_look')->where(['user_id' => $val['user_id'], 'is_free' => 0])->setField('is_valid', 0);
        }
    }

    // 定时处理未分佣的记录
    public function createProfit(){
        file_put_contents(RUNTIME_PATH . 'create_profit.txt', date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
        $list = Db::name('order')->field('id,user_id,pay_price,service_rate,service_name,pay_time')->where(['service_rate' => ['gt', 0], 'pay_time' => ['gt', 1733222108], 'is_create_profit' => 0])->select();
        if($list){
            foreach($list as &$v){
                $order_id = $v['id'];
                $user_id = $v['user_id'];
                $pay_price = $v['pay_price'];
                $rate = $v['service_rate'];

                $up_user_id = Db::name('user')->where(['id' => $user_id])->value('up_user_id');

                if($up_user_id && $rate){
                    // 上级用户的角色
                    $up_user_info = Db::name('user')->field('role_id,all_subordinate_pay_price,all_profit,profit')->where(['id' => $up_user_id])->find();

                    // 所有下级用户累计消费的实付金额
                    $all_subordinate_pay_price = $up_user_info['all_subordinate_pay_price'];

                    Db::startTrans();
                    try {
                        $count = Db::name('profit')->where(['order_id' => $order_id])->count();
                        if($count == 0){
                            $profit = $pay_price * $rate / 100;
                            $profit_data = [
                                'user_id' => $up_user_id,
                                'order_id' => $order_id,
                                'pay_price' => $pay_price,
                                'rate' => $rate,
                                'profit' => $profit, // 分佣金额
                                'desc' => "开通${v['service_name']}，${rate}%提成",
                                'type' => 1,
                                'status' => 1,
                                'update_time' => $v['pay_time'],
                                'create_time' => $v['pay_time']
                            ];
                            Db::name('profit')->insert($profit_data);

                            // 4：修改上级用户的【总收益】【当前收益】【所有下级用户累计消费的实付金额】
                            $up_user_data = [
                                'all_profit' => sprintf('%.2f', $up_user_info['all_profit'] + $profit),
                                'profit' => sprintf('%.2f', $up_user_info['profit'] + $profit),
                                'all_subordinate_pay_price' => sprintf('%.2f', $all_subordinate_pay_price + $pay_price)
                            ];
                            Db::name('user')->where(['id' => $up_user_id])->update($up_user_data);

                            Db::commit();
                            file_put_contents(RUNTIME_PATH . 'plan_profit_success.txt', 'order_id：' . $v['id'] . '成功延迟产生抽佣记录---' . date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
                        }else{
                            file_put_contents(RUNTIME_PATH . 'plan_profit_success_again.txt', 'order_id：' . $v['id'] . '尝试重复抽佣，但未能成功' . PHP_EOL, FILE_APPEND);
                        }
                    }catch (Exception $e){
                        Db::rollback();
                        file_put_contents(RUNTIME_PATH . 'plan_profit_error.txt', 'order_id：' . $v['id'] . '失败延迟产生抽佣记录---' . date('Y-m-d H:i:s') . ' ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
                    }
                }
            }
        }
    }
}